package com.egao.orderB.service.impl;

import com.egao.common.core.utils.StringUtil;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.AddressMapper;
import com.egao.orderB.biz.MaintainFeeBiz;
import com.egao.orderB.entity.Address;
import com.egao.orderB.entity.MaintainFee;
import com.egao.orderB.service.AddressService;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.AddressBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 地址报价服务实现类
 * @author : Doman
 * @date : 2020-06-22 10:37:54
 * @version ：1.0
 */
@Service
public class AddressServiceImpl /*extends ServiceImpl<AddressMapper, Address> */implements AddressService {

	@Autowired
	private AddressBiz addressBiz;
	@Autowired
	private MaintainFeeBiz maintainFeeBiz;

    /**
    @Override
    public PageResult<Address> listPage(PageParam<Address> page) {
        List<Address> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Address> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JSONObject list(PageResult2<T> page, Address address, User loginUser)  throws Exception{
        try {
            return addressBiz.list(page,address,loginUser);
        } catch (Exception e) {
            throw new Exception("查询错误,请联系平台管理员!");
        }

    }

    @Override
    public List<Address> list(Address address, User loginUser) {
        return addressBiz.list(address,loginUser);
    }

    @Override
    public List<Address> searchAddressFee(Address address, User loginUser) {
        List<Address> addresseList = addressBiz.searchAddressFee(address,loginUser);
        if(addresseList != null && addresseList.size() > 0) {
            return addresseList;
        } else {
            MaintainFee maintainFee = new MaintainFee();
            maintainFee.setProvince(address.getCustomerName());
            return maintainFeeBiz.searchAddressFee(maintainFee,loginUser);
        }
    }

    @Override
    public List<Address> dockList(Address address, User loginUser) {
        return addressBiz.dockList(address,loginUser);
    }

    @Override
    public Address getById(Address address) {
        return addressBiz.getById(address);
    }

    @Transactional
    @Override
    public JsonResult save(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getCustomerId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        if (addressBiz.save(address, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        if (addressBiz.update(address, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
    @Transactional
    @Override
    public JsonResult customerUpdate(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        if (addressBiz.customerUpdate(address, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updatePrice(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        if (addressBiz.updatePrice(address, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }
    @Transactional
    @Override
    public JsonResult updateState(Address address, User loginUser) {
        if(StringUtil.isBlank(address.getId())) {
            return JsonResult.error("请填写完整的内容,操作失败!");
        }
        address.setState("已通过");
        if (addressBiz.updateState(address, loginUser)) {
            return JsonResult.ok("审核通过");
        }
        return JsonResult.error("审核失败");
    }

    @Transactional
    @Override
    public JsonResult remove(Address address, User loginUser) {
        if (addressBiz.remove(address, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
